using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Adex
{
	/// <summary>
	/// Strongly-typed collection for the Order class.
	/// </summary>
	[Serializable]
	public partial class OrderCollection : ActiveList<Order, OrderCollection> 
	{	   
		public OrderCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Order table.
	/// </summary>
	[Serializable]
	public partial class Order : ActiveRecord<Order>
	{
		#region .ctors and Default Settings
		
		public Order()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Order(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Order(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Order(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Order", TableType.Table, DataService.GetInstance("Adex"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarOrderID = new TableSchema.TableColumn(schema);
				colvarOrderID.ColumnName = "OrderID";
				colvarOrderID.DataType = DbType.Int32;
				colvarOrderID.MaxLength = 0;
				colvarOrderID.AutoIncrement = true;
				colvarOrderID.IsNullable = false;
				colvarOrderID.IsPrimaryKey = true;
				colvarOrderID.IsForeignKey = false;
				colvarOrderID.IsReadOnly = false;
				colvarOrderID.DefaultSetting = @"";
				colvarOrderID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarOrderID);
				
				TableSchema.TableColumn colvarSiteUserID = new TableSchema.TableColumn(schema);
				colvarSiteUserID.ColumnName = "SiteUserID";
				colvarSiteUserID.DataType = DbType.Int32;
				colvarSiteUserID.MaxLength = 0;
				colvarSiteUserID.AutoIncrement = false;
				colvarSiteUserID.IsNullable = true;
				colvarSiteUserID.IsPrimaryKey = false;
				colvarSiteUserID.IsForeignKey = true;
				colvarSiteUserID.IsReadOnly = false;
				colvarSiteUserID.DefaultSetting = @"";
				
					colvarSiteUserID.ForeignKeyTableName = "SiteUser";
				schema.Columns.Add(colvarSiteUserID);
				
				TableSchema.TableColumn colvarCompanyCode = new TableSchema.TableColumn(schema);
				colvarCompanyCode.ColumnName = "CompanyCode";
				colvarCompanyCode.DataType = DbType.Int32;
				colvarCompanyCode.MaxLength = 0;
				colvarCompanyCode.AutoIncrement = false;
				colvarCompanyCode.IsNullable = true;
				colvarCompanyCode.IsPrimaryKey = false;
				colvarCompanyCode.IsForeignKey = true;
				colvarCompanyCode.IsReadOnly = false;
				colvarCompanyCode.DefaultSetting = @"";
				
					colvarCompanyCode.ForeignKeyTableName = "Company";
				schema.Columns.Add(colvarCompanyCode);
				
				TableSchema.TableColumn colvarOrderStatus = new TableSchema.TableColumn(schema);
				colvarOrderStatus.ColumnName = "OrderStatus";
				colvarOrderStatus.DataType = DbType.Byte;
				colvarOrderStatus.MaxLength = 0;
				colvarOrderStatus.AutoIncrement = false;
				colvarOrderStatus.IsNullable = true;
				colvarOrderStatus.IsPrimaryKey = false;
				colvarOrderStatus.IsForeignKey = false;
				colvarOrderStatus.IsReadOnly = false;
				colvarOrderStatus.DefaultSetting = @"";
				colvarOrderStatus.ForeignKeyTableName = "";
				schema.Columns.Add(colvarOrderStatus);
				
				TableSchema.TableColumn colvarOrderTotal = new TableSchema.TableColumn(schema);
				colvarOrderTotal.ColumnName = "OrderTotal";
				colvarOrderTotal.DataType = DbType.Currency;
				colvarOrderTotal.MaxLength = 0;
				colvarOrderTotal.AutoIncrement = false;
				colvarOrderTotal.IsNullable = false;
				colvarOrderTotal.IsPrimaryKey = false;
				colvarOrderTotal.IsForeignKey = false;
				colvarOrderTotal.IsReadOnly = false;
				colvarOrderTotal.DefaultSetting = @"";
				colvarOrderTotal.ForeignKeyTableName = "";
				schema.Columns.Add(colvarOrderTotal);
				
				TableSchema.TableColumn colvarCompanyUnlimitedCredits = new TableSchema.TableColumn(schema);
				colvarCompanyUnlimitedCredits.ColumnName = "CompanyUnlimitedCredits";
				colvarCompanyUnlimitedCredits.DataType = DbType.Boolean;
				colvarCompanyUnlimitedCredits.MaxLength = 0;
				colvarCompanyUnlimitedCredits.AutoIncrement = false;
				colvarCompanyUnlimitedCredits.IsNullable = false;
				colvarCompanyUnlimitedCredits.IsPrimaryKey = false;
				colvarCompanyUnlimitedCredits.IsForeignKey = false;
				colvarCompanyUnlimitedCredits.IsReadOnly = false;
				colvarCompanyUnlimitedCredits.DefaultSetting = @"";
				colvarCompanyUnlimitedCredits.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCompanyUnlimitedCredits);
				
				TableSchema.TableColumn colvarPricePerSecondOnOrder = new TableSchema.TableColumn(schema);
				colvarPricePerSecondOnOrder.ColumnName = "PricePerSecondOnOrder";
				colvarPricePerSecondOnOrder.DataType = DbType.Currency;
				colvarPricePerSecondOnOrder.MaxLength = 0;
				colvarPricePerSecondOnOrder.AutoIncrement = false;
				colvarPricePerSecondOnOrder.IsNullable = true;
				colvarPricePerSecondOnOrder.IsPrimaryKey = false;
				colvarPricePerSecondOnOrder.IsForeignKey = false;
				colvarPricePerSecondOnOrder.IsReadOnly = false;
				colvarPricePerSecondOnOrder.DefaultSetting = @"";
				colvarPricePerSecondOnOrder.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPricePerSecondOnOrder);
				
				TableSchema.TableColumn colvarDateAdded = new TableSchema.TableColumn(schema);
				colvarDateAdded.ColumnName = "DateAdded";
				colvarDateAdded.DataType = DbType.DateTime;
				colvarDateAdded.MaxLength = 0;
				colvarDateAdded.AutoIncrement = false;
				colvarDateAdded.IsNullable = false;
				colvarDateAdded.IsPrimaryKey = false;
				colvarDateAdded.IsForeignKey = false;
				colvarDateAdded.IsReadOnly = false;
				colvarDateAdded.DefaultSetting = @"";
				colvarDateAdded.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateAdded);
				
				TableSchema.TableColumn colvarDateModified = new TableSchema.TableColumn(schema);
				colvarDateModified.ColumnName = "DateModified";
				colvarDateModified.DataType = DbType.DateTime;
				colvarDateModified.MaxLength = 0;
				colvarDateModified.AutoIncrement = false;
				colvarDateModified.IsNullable = false;
				colvarDateModified.IsPrimaryKey = false;
				colvarDateModified.IsForeignKey = false;
				colvarDateModified.IsReadOnly = false;
				colvarDateModified.DefaultSetting = @"";
				colvarDateModified.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateModified);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["Adex"].AddSchema("Order",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("OrderID")]
		public int OrderID 
		{
			get { return GetColumnValue<int>("OrderID"); }

			set { SetColumnValue("OrderID", value); }

		}

		  
		[XmlAttribute("SiteUserID")]
		public int? SiteUserID 
		{
			get { return GetColumnValue<int?>("SiteUserID"); }

			set { SetColumnValue("SiteUserID", value); }

		}

		  
		[XmlAttribute("CompanyCode")]
		public int? CompanyCode 
		{
			get { return GetColumnValue<int?>("CompanyCode"); }

			set { SetColumnValue("CompanyCode", value); }

		}

		  
		[XmlAttribute("OrderStatus")]
		public byte? OrderStatus 
		{
			get { return GetColumnValue<byte?>("OrderStatus"); }

			set { SetColumnValue("OrderStatus", value); }

		}

		  
		[XmlAttribute("OrderTotal")]
		public decimal OrderTotal 
		{
			get { return GetColumnValue<decimal>("OrderTotal"); }

			set { SetColumnValue("OrderTotal", value); }

		}

		  
		[XmlAttribute("CompanyUnlimitedCredits")]
		public bool CompanyUnlimitedCredits 
		{
			get { return GetColumnValue<bool>("CompanyUnlimitedCredits"); }

			set { SetColumnValue("CompanyUnlimitedCredits", value); }

		}

		  
		[XmlAttribute("PricePerSecondOnOrder")]
		public decimal? PricePerSecondOnOrder 
		{
			get { return GetColumnValue<decimal?>("PricePerSecondOnOrder"); }

			set { SetColumnValue("PricePerSecondOnOrder", value); }

		}

		  
		[XmlAttribute("DateAdded")]
		public DateTime DateAdded 
		{
			get { return GetColumnValue<DateTime>("DateAdded"); }

			set { SetColumnValue("DateAdded", value); }

		}

		  
		[XmlAttribute("DateModified")]
		public DateTime DateModified 
		{
			get { return GetColumnValue<DateTime>("DateModified"); }

			set { SetColumnValue("DateModified", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Adex.OrderDetailCollection OrderDetailRecords()
		{
			return new Adex.OrderDetailCollection().Where(OrderDetail.Columns.OrderID, OrderID).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Company ActiveRecord object related to this Order
		/// 
		/// </summary>
		public Adex.Company Company
		{
			get { return Adex.Company.FetchByID(this.CompanyCode); }

			set { SetColumnValue("CompanyCode", value.CompanyCode); }

		}

		
		
		/// <summary>
		/// Returns a SiteUser ActiveRecord object related to this Order
		/// 
		/// </summary>
		public Adex.SiteUser SiteUser
		{
			get { return Adex.SiteUser.FetchByID(this.SiteUserID); }

			set { SetColumnValue("SiteUserID", value.SiteUserID); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int? varSiteUserID,int? varCompanyCode,byte? varOrderStatus,decimal varOrderTotal,bool varCompanyUnlimitedCredits,decimal? varPricePerSecondOnOrder,DateTime varDateAdded,DateTime varDateModified)
		{
			Order item = new Order();
			
			item.SiteUserID = varSiteUserID;
			
			item.CompanyCode = varCompanyCode;
			
			item.OrderStatus = varOrderStatus;
			
			item.OrderTotal = varOrderTotal;
			
			item.CompanyUnlimitedCredits = varCompanyUnlimitedCredits;
			
			item.PricePerSecondOnOrder = varPricePerSecondOnOrder;
			
			item.DateAdded = varDateAdded;
			
			item.DateModified = varDateModified;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varOrderID,int? varSiteUserID,int? varCompanyCode,byte? varOrderStatus,decimal varOrderTotal,bool varCompanyUnlimitedCredits,decimal? varPricePerSecondOnOrder,DateTime varDateAdded,DateTime varDateModified)
		{
			Order item = new Order();
			
				item.OrderID = varOrderID;
				
				item.SiteUserID = varSiteUserID;
				
				item.CompanyCode = varCompanyCode;
				
				item.OrderStatus = varOrderStatus;
				
				item.OrderTotal = varOrderTotal;
				
				item.CompanyUnlimitedCredits = varCompanyUnlimitedCredits;
				
				item.PricePerSecondOnOrder = varPricePerSecondOnOrder;
				
				item.DateAdded = varDateAdded;
				
				item.DateModified = varDateModified;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string OrderID = @"OrderID";
			 public static string SiteUserID = @"SiteUserID";
			 public static string CompanyCode = @"CompanyCode";
			 public static string OrderStatus = @"OrderStatus";
			 public static string OrderTotal = @"OrderTotal";
			 public static string CompanyUnlimitedCredits = @"CompanyUnlimitedCredits";
			 public static string PricePerSecondOnOrder = @"PricePerSecondOnOrder";
			 public static string DateAdded = @"DateAdded";
			 public static string DateModified = @"DateModified";
						
		}

		#endregion
	}

}

